/*
 * Date：10/23/18 10:08 PM
 * Author: guomw
 *
 */

package com.treasure.mall.biz.repository.system;

import com.treasure.mall.biz.entity.auth.SystemMenu;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 系统菜单Repository
 *
 * @author guomw
 * @date 2018/9/17
 */
@Repository
public interface SystemMenuRepository extends JpaRepository<SystemMenu, Integer>, JpaSpecificationExecutor<SystemMenu> {

    /**
     * 更新启用状态
     *
     * @param itemCode
     * @param enable
     * @return
     */
    @Query("update SystemMenu m set m.itemEnable=?2 where m.itemCode=?1")
    @Modifying(clearAutomatically = true)
    @Transactional(rollbackFor = RuntimeException.class)
    int updateEnableByItemCode(Integer itemCode, boolean enable);

    /**
     * 根据菜单ID，获取菜单数据
     *
     * @param menuIds
     * @param itemFunction 是否是功能菜单
     * @return
     */
    @Query("select u from SystemMenu u where u.itemCode in ?1 and u.itemFunction=?2")
    List<SystemMenu> findInMenuIds(List<Integer> menuIds, boolean itemFunction);

    /**
     * 根据菜单ID，获取菜单数据
     *
     * @param menuIds
     * @return
     */
    @Query("select u from SystemMenu u where u.itemCode in ?1")
    List<SystemMenu> findInMenuIds(List<Integer> menuIds);

    /**
     * 根据路径获取菜单
     *
     * @param itemPath
     * @return
     */
    @Query("select u from SystemMenu u where u.itemPath=?1")
    SystemMenu findByItemPath(String itemPath);
}
